<?php
abstract class Controller_Abstract
{
	protected $_viewName;
	protected $_app;
	protected $_view = array();
	
	public function __construct($app)
	{
		$this->_app = $app;
	}
	
	protected function _before_execute()
	{
		session_start();
		$this->_view['js_url'] = 'http://' . $this->_app->site['host'] . '/js';
		$this->_view['css_url'] = 'http://' . $this->_app->site['host'] . '/css';
		$this->_view['image_url'] = 'http://' . $this->_app->site['host'] . '/images';
	}
	
	protected function _after_execute()
	{
		
	}
    
    
	protected function _assign()
	{
		extract($this->_view);
		$viewFile = dirname(dirname(__FILE__)) . '/view/' . $this->_app->controllerName . '/' . $this->_viewName . '.php';
		if(file_exists($viewFile)){
			include_once $viewFile;
		}
		
	}
	
    protected function _setViewname($actionName)
    {
    	$this->_viewName = $actionName;
    }
    
    
    protected function _redirect($url, $delay = 0)
    {
    	if($delay > 0){
    		echo <<<EOT
<html>
	<head>
		<meta http-equiv="refresh" content="{$delay}" URL="{$url}" />
	</head>
</html>
EOT;
    	}else{
    		header("Location: {$url}");
    	}
    }
    
    
    
    
    
    /*************************/
    public function existsAction($action_name)
    {
        return method_exists($this, $action_name);
    }
	
	public function execute($actionName, array $args = array())
	{
		$viewName = $actionName;
		$actionName = $actionName . 'Action';
		$this->_before_execute();
		$response = $this->$actionName();
		if(is_string($response)){
			return $response;
		}
		$this->_after_execute();
        //$this->_before_render($response);
        $this->_setViewname($viewName);
        $this->_assign();
	}
    
    
    
}